﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;

namespace DAO
{
    public class BanAnDAO
    {
        public DataTable LoadDS()
        {
            Data a = new Data();
            string sql = "SELECT * FROM BanAn where IsDelete = 0";
            DataTable dt = new DataTable();
            dt = a.ExecuteQuery(sql);

            return dt;
        }

        public Boolean CapNhat(BanAnDTO baDTO)
        {
            Data a = new Data();
            string sql = "Update BanAn set TenBan = N'" + baDTO.TenBan + "', SucChua = " + baDTO.SucChua + " where MaBan = " + baDTO.MaBan + " ";

            return a.ExecuteNonQuery(sql);
        }

        //public Boolean KiemTraTrungTenBan(string tenban)
        //{
        //    Data a = new Data();
        //    string sql = "select * from BanAn where TenBan = N'" + tenban + "'";
        //    //if (a.ExecuteQuery(sql).Rows.Count > 0)
        //        //return true;
        //    string sql2="select TenBan from BanAn";
            
        //    DataTable dtb=  a.ExecuteQuery(sql2);

        //    //int check;
        //    foreach (DataRow x in dtb.Rows )
        //    {
             

        //        if (string.Compare(x[0].ToString().Trim(' ').ToLower(), tenban.Trim(' ').ToLower())==0||(x[0].ToString().Trim(' ').ToLower().Contains(tenban.Trim(' ').ToLower())||(tenban.Trim(' ').ToLower().Contains(x[0].ToString().Trim(' ').ToLower()))))
        //        {
        //            return true;
        //        }
        //    }
           
        //    return false;
        //}

        public DataTable DanhSachTenBan()
        {
            Data dt = new Data();
            string sql = "select TenBan from BanAn";
           return dt.ExecuteQuery(sql);

        }

        public Boolean CapNhatBanTrung(string tenban, int succhua)
        {
            Data a = new Data();
            string sql = "Update BanAn set IsDelete = 0, SucChua = " + succhua + " where TenBan = N'" + tenban + "'";
            return a.ExecuteNonQuery(sql);
        }

        public Boolean ThemBan(BanAnDTO baDTO)
        {
            Data a = new Data();
            string sql = "insert into BanAn(TenBan, SucChua,IsDelete) ";
            sql += "values( N'" + baDTO.TenBan + "',";
            sql += " " + baDTO.SucChua + ",0 )";

            return a.ExecuteNonQuery(sql);
        }

        public Boolean XoaBan(int maBan)
        {
            Data a = new Data();
            string sql = "Update BanAn set IsDelete = 1 where MaBan = '" + maBan + "'";

            return a.ExecuteNonQuery(sql);
        }
    }
}
